<template>
  <div class="content">
    <div class="header">
      <h3 class="title">待审核列表</h3>
    </div>
    <div class="table">
      <el-table :data="clubsList" border style="width: 100%">
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60px"
        />

        <el-table-column
          align="center"
          label="社团名称"
          prop="name"
          width="150px"
        />

        <el-table-column
          align="center"
          label="社长"
          prop="leaderName"
          width="150px"
        />
        <el-table-column align="center" label="社团状态" prop="status">
          <template #default="scope">
            <el-button size="small" type="danger">
              {{ returnStatus(scope) }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="社团头像" prop="avatar_url">
          <template #default="scope">
            <img style="width: 50px" :src="scope.row.club_avatar" alt="" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="created_at">
          <template #default="scope">
            {{ formatUTC(scope.row.created_at) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template #default="scope">
            <el-button
              size="small"
              icon="check"
              type="primary"
              text
              @click="handleAgreeBtnClick(scope.row.id)"
            >
              <!-- @click="handleEditBtnClick(scope.row)" -->
              同意
            </el-button>
            <el-button
              v-if="scope.row.status === 'pending'"
              size="small"
              icon="close"
              type="warning"
              text
              @click="handleRejectBtnClick(scope.row.id)"
            >
              驳回
            </el-button>
            <el-button
              size="small"
              icon="delete"
              type="danger"
              text
              @click="handleDeleteBtnClick(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="clubsTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import useClubStore from '@/store/main/club/club'
import { formatUTC } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'

// 1.发起action，请求usersList的数据
const clubStore = useClubStore()
const currentPage = ref(1)
const pageSize = ref(10)

// 渲染不同的社团状态
// 渲染不同的状态
function returnStatus(scope: any) {
  let status = ''
  if (scope.row.status === 'approved') {
    status = '激活'
  } else if (scope.row.status === 'pending') {
    status = '待审核'
  } else {
    status = '已驳回'
  }
  return status
}

fetchClubListData()
// 2.获取usersList数据,进行展示
const { clubsList, clubsTotalCount } = storeToRefs(clubStore)

// 3.页码相关的逻辑
function handleSizeChange() {
  fetchClubListData()
}
function handleCurrentChange() {
  fetchClubListData()
}

// 4.定义函数, 用于发送网络请求
function fetchClubListData(formData?: any) {
  // 1.获取offset/size
  const limit = pageSize.value
  const offset = (currentPage.value - 1) * limit
  // 2.发起网络请求
  if (formData) {
    clubStore.getClubListByNameAction(
      formData.name,
      limit,
      offset,
      formData.status
    )
    return
  }
  clubStore.getClubsListAction(limit, offset, 'pending')
}

// 同意创建社团
async function handleAgreeBtnClick(id: number) {
  await clubStore.updateClubStatusAction(id, 'approved')
  ElMessage({
    type: 'success',
    message: '审核社团成功',
    duration: 1000
  })
  fetchClubListData()
}
// 驳回社团
async function handleRejectBtnClick(id: number) {
  ElMessageBox.confirm(`确认驳回这个社团吗`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await clubStore.updateClubStatusAction(id, 'rejected')
    ElMessage({
      type: 'success',
      message: '驳回社团成功',
      duration: 1000
    })
    fetchClubListData()
  })
}

// 删除数据
async function handleDeleteBtnClick(id: number) {
  ElMessageBox.confirm(`确认删除这个社团吗`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await clubStore.deleteClubByIdAction(id)
    ElMessage({
      type: 'success',
      message: '删除社团成功',
      duration: 1000
    })
    fetchClubListData()
  })
}

defineExpose({
  fetchClubListData
})
</script>

<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;

  .title {
    font-size: 22px;
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
